package org.tamal.persistence;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Represents a user that maps to a record in 'users' table.
 * @author Tamal Kanti Nath
 */
@Entity
@Table(name = "users")
public class User implements Serializable {
    private static final long serialVersionUID = -8901555284486314868L;
    private long id;
    private String email;
    private String password;
    private String name;
    private Character provider;
    private Date lastLogin;
    private Date lastLoginFailed;
    private int loginAttempts;
    private Date passwordChanged;

    /**
     * Returns the id.
     * @return the id
     */
    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }

    /**
     * Sets the id.
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Returns the email.
     * @return the email
     */
    @Column(nullable = false, unique = true, updatable = false, length = 40)
    public String getEmail() {
        return email;
    }

    /**
     * Sets the email.
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns the password.
     * @return the password
     */
    @Column(length = 16)
    public String getPassword() {
        return password;
    }

    /**
     * Sets the provider.
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Returns the name.
     * @return the name
     */
    @Column(nullable = false, length = 30)
    public String getName() {
        return name;
    }

    /**
     * Sets the name.
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the provider.
     * @return the provider
     */
    @Column(length = 1)
    public Character getProvider() {
        return provider;
    }

    /**
     * Sets the provider.
     * @param provider the provider to set
     */
    public void setProvider(Character provider) {
        this.provider = provider;
    }

    /**
     * Returns the lastLogin.
     * @return the lastLogin
     */
    @Column
    public Date getLastLogin() {
        return lastLogin;
    }

    /**
     * Sets the lastLogin.
     * @param lastLogin the lastLogin to set
     */
    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    /**
     * Returns the lastLoginFailed.
     * @return the lastLoginFailed
     */
    @Column
    public Date getLastLoginFailed() {
        return lastLoginFailed;
    }

    /**
     * Sets the lastLoginFailed.
     * @param lastLoginFailed the lastLoginFailed to set
     */
    public void setLastLoginFailed(Date lastLoginFailed) {
        this.lastLoginFailed = lastLoginFailed;
    }

    /**
     * Returns the count of last consecutive unsuccessful login attempts.
     * @return the loginAttempts
     */
    @Column(nullable = false)
    public int getLoginAttempts() {
        return loginAttempts;
    }

    /**
     * Sets the count of last consecutive unsuccessful login attempts.
     * @param loginAttempts the loginAttempts to set
     */
    public void setLoginAttempts(int loginAttempts) {
        this.loginAttempts = loginAttempts;
    }

    /**
     * Returns the passwordChanged.
     * @return the passwordChanged
     */
    @Column
    public Date getPasswordChanged() {
        return passwordChanged;
    }

    /**
     * Sets the passwordChanged.
     * @param passwordChanged the passwordChanged to set
     */
    public void setPasswordChanged(Date passwordChanged) {
        this.passwordChanged = passwordChanged;
    }

    /**
     * Copy from given user.
     * @param user the reference user
     */
    public void copy(User user) {
        id = user.id;
        email = user.email;
        password = user.password;
        name = user.name;
        provider = user.provider;
        lastLogin = user.lastLogin;
        lastLoginFailed = user.lastLoginFailed;
        loginAttempts = user.loginAttempts;
        passwordChanged = user.passwordChanged;
    }

    /**
     * Overrides the toString() method of the Object.
     * @return the String representation of User.
     * @see java.lang.Object
     */
    @Override
    public String toString() {
        return name;
    }

}
